// pages/sub-category/sub-category.js

var http = require("../../utils/http.js");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    subCategoryList: [],
    categoryId: 0,
    prodList: [],
    current: 1,
    pages: 0
  },

  // 获取顶栏子分类数据
  getSubCategory() {
    var params = {
      url: "/category/categoryInfo",
      method: "GET",
      data: {
        parentId: this.data.parentId
      },
      callBack: (res) => {
        this.setData({
          subCategoryList: res,
        });
      }
    };
    http.request(params);
  },

  // 根据分类id获取商品数据
  getProdList() {
    var params = {
      url: "/search/searchProdPage",
      method: "GET",
      data: {
        categoryId: this.data.categoryId,
        current: this.data.current,
        size: 10,
        sort: 0,
        isAllProdType: true,
      },
      callBack: (res) => {
        this.setData({
          prodList: res.current==1 ? res.records : this.data.prodList.concat(res.records),
          pages: res.pages
        });
      }
    };
    http.request(params);
  },

  // 切换子分类tab
  onSubCategoryTap(e) {
    this.setData({
      categoryId: e.currentTarget.dataset.id,
      current: 1,
      pages: 0,
      intoView: 'sw' + e.currentTarget.dataset.id,
    })
    this.getProdList()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.setData({
      parentId: options.parentId,
      categoryId: options.categoryId,
      intoView: 'sw'+ options.categoryId
    })
    this.getSubCategory()
    this.getProdList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if(this.data.current < this.data.pages){
      this.setData({
        current: this.data.current + 1,
      })
      this.getProdList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})